home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr48 / btv115.zip / EXAMPLE3.PAS < prev    next >
Pascal/Delphi Source File  |  1993-04-01  |  4KB  |  136 lines

  1. {$X+}
  2. {$V-}
  3. { EXAMPLE3.PAS - demonstrate file creation with variable length records,
  4.                  alternate collating sequences,
  5.                  reading and writing variable length records
  6.  
  7.   Requires Turbo Pascal version 6.0
  8. }
  9. Uses
  10.   Crt,
  11.   Dos,
  12.   Btrv6,
  13.   Btv;
  14.  
  15.  
  16. type
  17.   ErrorType = Object(ErrorDisplay)
  18.     Function    Display(Error     : Integer;
  19.                         ErrorMsg  : String;
  20.                         OpCode    : Byte;
  21.                         OpCodeMsg : String;
  22.                         FileName  : PathStr
  23.                         ): ErrorAction;             Virtual;
  24.   end;
  25.  
  26.  
  27. var
  28.   F           : BtrieveFile;
  29.   Buff        : record
  30.                   Name    : String[30];
  31.                   Comment : String[80];
  32.                 end;
  33.   Name        : String[30];
  34.   Comment     : String[80];
  35.   ErrHandler  : ErrorHandler;
  36.   ErrDisplay  : ErrorType;
  37.  
  38.  
  39. { Heres our error display object  }
  40. Function ErrorType.Display(Error     : Integer;
  41.                            ErrorMsg  : String;
  42.                            OpCode    : Byte;
  43.                            OpCodeMsg : String;
  44.                            FileName  : PathStr
  45.                            ): ErrorAction;
  46.   begin
  47.     ClrScr;
  48.     Writeln('Btrieve IO error for ' + FileName);
  49.     Writeln(Error,  ' - ', ErrorMsg);
  50.     Writeln(Opcode, ' - ', OpCodeMsg);
  51.     Writeln('Press any key ....');
  52.     ReadKey;
  53.     Display := erAbort; { halt program on any errors }
  54.     ClrScr;
  55.   end;
  56.  
  57. begin
  58.   { first make a error display }
  59.   ErrDisplay.Init;
  60.   { now make an error handler, it needs a display object  }
  61.   ErrHandler.Init(@ErrDisplay);
  62.  
  63.   ClrScr;
  64.   Writeln('Creating a file called TEST3.DAT');
  65.  
  66.   { init the file passing it the error handler and }
  67.   { address of our data buffer                     }
  68.   F.Init('TEST3.DAT', @ErrHandler, @Buff, SizeOf(Buff));
  69.  
  70.   { the first thing to do is define the key }
  71.   { key is name, it is an lString and uses an Alt. collating  }
  72.   { sequence and is left justified and padded                 }
  73.   F.AddKeySegment(1, 31, bExtended + bAltSequence, bLstring, 0, bLJustify);
  74.  
  75.   { define an alternate collating sequence }
  76.   F.AddAltSequence('UPPER.ALT');
  77.   { now that the key is defined lets create and open it }
  78.   { this one has variable length records                }
  79.   { the record size is only the size of fixed portion   }
  80.   F.Create(bVariableLen, 31, 1024, 0, bNormal);
  81.   F.Open(bNormal, '');
  82.  
  83.   if (F.bResult <> bOkay) then Halt;
  84.  
  85.   { lets add a couple records  }
  86.   Buff.Name   := 'AAAAAAAAAA';
  87.   Buff.Comment:= 'XXXXX';
  88.   { need to set the size of the output buffer, this must include the }
  89.   { fixed plus the variable portions                                 }
  90.   F.SetOutputSize(SizeOf(Buff.Name) + Length(Buff.Comment) + 1);
  91.   F.Insert;
  92.   Write('Adding some records .');
  93.   Delay(500);
  94.  
  95.   Buff.Name   := 'BBBBBBBBBB';
  96.   Buff.Comment:= 'XXXXXXXXXX';
  97.   F.SetOutputSize(SizeOf(Buff.Name) + Length(Buff.Comment) + 1);
  98.   F.Insert;
  99.   Write('.');
  100.   Delay(500);
  101.  
  102.   Buff.Name   := 'CCCCCCCCCC';
  103.   Buff.Comment:= 'XXXXXXXXXXXXXXX';
  104.   F.SetOutputSize(SizeOf(Buff.Name) + Length(Buff.Comment) + 1);
  105.   F.Insert;
  106.   Write('.');
  107.   Delay(500);
  108.  
  109.   Buff.Name   := 'DDDDDDDDDD';
  110.   Buff.Comment:= 'XXXXXXXXXXXXXXXXXXXX';
  111.   F.SetOutputSize(SizeOf(Buff.Name) + Length(Buff.Comment) + 1);
  112.   F.Insert;
  113.   Write('.');
  114.   Delay(500);
  115.  
  116.   Buff.Name   := 'EEEEEEEEEE';
  117.   Buff.Comment:= 'XXXXXXXXXXXXXXXXXXXXXXXXX';
  118.   F.SetOutputSize(SizeOf(Buff.Name) + Length(Buff.Comment) + 1);
  119.   F.Insert;
  120.   Writeln('.');
  121.  
  122.   { now we'll just spin through the file, look at the data and }
  123.   { see how big each record is                                 }
  124.   F.Get(bGetFirst, bNoLock);
  125.  
  126.   While (F.bResult = bOkay) do
  127.   begin
  128.     Writeln(Buff.Name);
  129.     Writeln(Buff.Comment);
  130.     Writeln(F.BytesRead, ' Bytes in this record');
  131.     Writeln;
  132.     F.Get(bGetNext, bNoLock);
  133.   end;
  134.  
  135.   F.Close;
  136. end.